package com.ellricky.baselib.ui.consutant

import android.util.Log
import android.view.View
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.EmptyUtil
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.entity.IdNamePickerEntity
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEntityCallback
import com.ellricky.baselib.widget.EditItemView
import com.flyco.roundview.RoundTextView
import java.util.*

class CtSchoolSelectActivity : BaseToolBarActivity<Void, BasePresenter<Void>>() {

    private var etSchool: EditItemView? = null
    private var btnConfirm: RoundTextView? = null
    private var id: String? = null

    private var ePickerUtil: EPickerUtil? = null
    private var selCt: IdNamePickerEntity? = null

    override fun getContentLayout(): Int {
        return R.layout.activity_ct_school_select
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.setText(R.string.base_change_ct)

        id = intent.getStringExtra(BaseConstant.KEY_ID)

        etSchool = findViewById(R.id.etSchool)
        btnConfirm = findViewById(R.id.btnConfirm)

        ePickerUtil = EPickerUtil(this)

        etSchool?.setOnClickListener(this)
        btnConfirm?.setOnClickListener(this)
    }

    override fun onClickView(v: View?) {
        super.onClickView(v)
        if (v === etSchool) {
            ePickerUtil?.checkSchoolDate(selCt) { _, t ->
                selCt = t
                etSchool?.value = t?.name
            }
            return
        }

        if (v === btnConfirm) {
            postSubmit()
        }
    }

    private fun postSubmit() {
        val ct: String = etSchool?.value ?: ""
        if (EmptyUtil.isEmpty(ct)) {
            ToastUtil.show(this, R.string.base_please_select_school)
            return
        }
        Log.e("ids", id.toString())
        val url = EAppUtil.getApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = "allotClueToSchool"
        paramMap["clue_ids"] = id!!
        paramMap["school_id"] = selCt?.id ?: ""
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance().postFrom(url, paramMap, object : NewEntityCallback<Any?>(this) {
            override fun onSuccess(var1: Any?, var2: String?) {
                setResult(BaseConstant.RESULT_CODE_ADD)
                finish()
            }
        })
    }
}